package io.kiki.sba.registry.server.data_store.remoting.metaserver.provideData.processor;

import io.kiki.sba.registry.common.model.constants.ValueConstants;
import io.kiki.sba.registry.common.model.metaserver.ProvideData;
import io.kiki.sba.registry.server.data_store.bootstrap.ServerConfig;
import io.kiki.sba.registry.server.data_store.lease.ClientInterfaceLeaseManager;
import io.kiki.sba.registry.server.shared.providedata.ProvideDataProcessor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;


public class SessionLeaseProvideDataProcessor implements ProvideDataProcessor {
    private static final Logger logger = LoggerFactory.getLogger(SessionLeaseProvideDataProcessor.class);

    @Autowired
    private ServerConfig serverConfig;

    @Override
    public boolean processData(ProvideData provideData) {
        if (provideData == null) {
            logger.info("Fetch data sessionLease null");
            return false;
        }

        final Integer data = ProvideData.toInteger(provideData);
        if (data == null) {
            logger.info("Fetch data sessionLease content null");
            return false;
        }

        logger.info("Fetch sessionLeaseSec {}", data);
        ClientInterfaceLeaseManager.validateSessionLeaseSec(data);
        serverConfig.setSessionLeaseSecs(data);

        return true;
    }


    @Override
    public boolean support(String dataInfoId) {
        return ValueConstants.DATA_SESSION_LEASE_SEC.equals(dataInfoId);
    }
}
